package com.shiny.widgetsdk.image;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.widget.ImageView;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.shiny.widgetsdklibrary.R;

/**
 * 图片加载器
 */
public class ImageLoaderManager {
    /**
     * 默认的参数值
     */
    private static final int THREAD_COUNT = 4;
    private static final int PRIORITY = 2;
    private static final int DISK_CACHE_SIZE = 50 * 1024 * 1024;
    private static final int CONNECT_TIME_OUT = 5 * 1000;
    private static final int READ_TIME_OUT = 30 * 1000;

    private static ImageLoader mImageLoader = null;
    private volatile static ImageLoaderManager mInstance = null;

    public static ImageLoaderManager getInstance(Context context){
        if ( mInstance == null ) {
            synchronized (ImageLoaderManager.class){
                if (mInstance == null) {
                    mInstance = new ImageLoaderManager(context);
                }
            }
        }
        return mInstance;
    }

    private ImageLoaderManager(Context context){
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(context)
                .threadPoolSize(THREAD_COUNT) //线程最大数
                .threadPriority(Thread.NORM_PRIORITY-PRIORITY) //线程优先级
                .denyCacheImageMultipleSizesInMemory()  //防止缓存多套尺寸的图片到我们的内存中
                .memoryCache(new WeakMemoryCache())     //使用弱引用内存缓存
                .diskCacheSize(DISK_CACHE_SIZE)         //分配硬盘缓存大小
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())     //使用MD5命令文件
                .tasksProcessingOrder(QueueProcessingType.LIFO)     //图片下载顺序
                .defaultDisplayImageOptions(getDefaultOptions())     //默认的图片加载options
                .imageDownloader(new BaseImageDownloader(context, CONNECT_TIME_OUT,READ_TIME_OUT))  //设置下载器
                .writeDebugLogs()  //debug环境输出日志
                .build();
        ImageLoader.getInstance().init(configuration);
        mImageLoader = ImageLoader.getInstance();
    }

    /**
     * @return默认的options
     */
    private DisplayImageOptions getDefaultOptions() {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.drawable.xadsdk_img_error)
                .showImageOnFail(R.drawable.xadsdk_img_error)
                .cacheInMemory(true)    //设置图片可以缓存在内存
                .cacheOnDisk(true)      //设置图片可以缓存在硬盘
                .bitmapConfig(Bitmap.Config.RGB_565)    //图片解码类型
                .decodingOptions(new BitmapFactory.Options())   //图片解码配置
                .build();
        return options;
    }

    /**
     * @param imageview
     * @param url
     * @param options
     * @param listener
     * @exception:显示图片
     */
    public void displayImage(ImageView imageview, String url,
                             DisplayImageOptions options,
                             ImageLoadingListener listener) {
        if ( mImageLoader != null ) {
            mImageLoader.displayImage(url, imageview, options, listener);
        }
    }

    public void displayImage(ImageView imageview, String url,
                             ImageLoadingListener listener) {
        displayImage(imageview,url,null,listener);
    }

    public void displayImage(ImageView imageview, String url) {
        displayImage(imageview,url,null,null);
    }

}


